﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xTeam.HengLe.Entity;
using Valor.DBUtility.MSSQLHelper;
using System.Data.SqlClient;

namespace xTeam.HengLe.DataAccess
{
    /// <summary>
    /// 留言、回复数据操作类
    /// </summary>
    public class LeaveWordReplyDataAccess
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private static string _con;
        public static string Con
        {
            get { return _con; }
            set { _con = value; }
        }

        public LeaveWordReplyDataAccess() { }

        #region 编辑留言、回复

        /// <summary>
        /// 编辑留言
        /// </summary>
        /// <param name="leaveWord">留言实体</param>
        /// <returns></returns>
        public static int EditLeaveWord(ELeaveWord leaveWord)
        {
            return Edit(leaveWord, 0);
        }
        /// <summary>
        /// 编辑回复
        /// </summary>
        /// <param name="leaveWord">回复实体</param>
        /// <returns></returns>
        public static int EditReply(ELeaveWord leaveWord)
        {
            return Edit(leaveWord, 1);
        }
        /// <summary>
        /// 编辑留言或回复
        /// </summary>
        /// <param name="leaveWord">留言或回复实体</param>
        /// <param name="flag">标识：0编辑留言，1编辑回复</param>
        /// <returns></returns>
        public static int Edit(ELeaveWord leaveWord, int flag)
        {
            // 存储过程参数值
            object[] paramvalues = new object[] { 
                leaveWord.RId, leaveWord.LId, leaveWord.Name, leaveWord.ContactWay, leaveWord.Content, leaveWord.Isvalid, flag };
            // 实例化存储过程操作类
            StoreProcedure sp = new StoreProcedure(_con, "leaveWord_reply_update");
            // 执行存储过程
            int ret = Convert.ToInt32(sp.ExecuteScalar(paramvalues));
            return ret;
        }

        #endregion
        #region 更新留言状态、删除留言、删除回复

        /// <summary>
        /// 更新留言状态
        /// </summary>
        /// <param name="lid">留言ID</param>
        /// <param name="isValid">留言状态</param>
        /// <returns></returns>
        public static int UpdateLeaveWordState(int lid, bool isValid)
        {
            return Delete(lid, isValid, 0);
        }
        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="lid">留言ID</param>
        /// <returns></returns>
        public static int DeleteLeaveWord(int lid)
        {
            return Delete(lid, true, 1);
        }
        /// <summary>
        /// 删除回复
        /// </summary>
        /// <param name="lid">回复ID</param>
        /// <returns></returns>
        public static int DeleteReply(int rid)
        {
            return Delete(rid, true, 2);
        }
        /// <summary>
        /// 更新留言状态、删除留言或删除回复
        /// </summary>
        /// <param name="lid">留言或回复ID</param>
        /// <param name="isValid">留言或回复状态</param>
        /// <param name="flag">标识：0更新留言状态，1删除留言，2删除回复</param>
        /// <returns></returns>
        public static int Delete(int lid, bool isValid, int flag)
        {
            // 存储过程参数值
            object[] paramvalues = new object[] { lid, isValid, flag };
            // 实例化存储过程操作类
            StoreProcedure sp = new StoreProcedure(_con, "leaveWord_reply_delete");
            // 执行存储过程
            int ret = sp.ExecuteNonQuery(paramvalues);
            return ret;
        }

        #endregion
        #region 查询留言、回复

        /// <summary>
        /// 根据留言ID查询留言
        /// </summary>
        /// <param name="lid">留言ID</param>
        /// <returns>某个留言</returns>
        public static ELeaveWord GetLeaveWordByLId(int lid)
        {
            int total = 0;
            List<ELeaveWord> leaveWordList = GetLeaveWordReply(lid, 0, 0, 2, ref total);

            if (leaveWordList.Count > 0)
                return leaveWordList[0];

            return null;
        }
        /// <summary>
        /// 根据回复ID查询留言
        /// </summary>
        /// <param name="rid">回复ID</param>
        /// <returns>某个回复</returns>
        public static ELeaveWord GetReplyByLId(int rid)
        {
            int total = 0;
            List<ELeaveWord> leaveWordList = GetLeaveWordReply(rid, 0, 0, 3, ref total);

            if (leaveWordList.Count > 0)
                return leaveWordList[0];

            return null;
        }
        /// <summary>
        /// 查询所有留言及回复
        /// </summary>
        /// <param name="lid">留言ID或回复ID</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="flag">标识：0查询所有有效留言及对应回复，1查询所有留言及对应回复</param>
        /// <param name="total">总记录数</param>
        /// <returns>所有留言及回复列表</returns>
        public static List<ELeaveWord> GetLeaveWordReply(int pageSize, int pageIndex, int flag, ref int total)
        {
            return GetLeaveWordReply(0, pageSize, pageIndex, flag, ref total);
        }
        /// <summary>
        /// 查询留言及回复、留言或回复
        /// </summary>
        /// <param name="lid">留言ID或回复ID</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="flag">标识：0查询所有有效留言及对应回复，1查询所有留言及对应回复，2查询留言，3查询回复</param>
        /// <param name="total">总记录数</param>
        /// <returns>留言及回复、留言或回复列表</returns>
        public static List<ELeaveWord> GetLeaveWordReply(int lid, int pageSize, int pageIndex, int flag, ref int total)
        {
            List<ELeaveWord> leaveWordList = new List<ELeaveWord>();
            // 存储过程参数值
            object[] paramvalues = new object[] { lid, pageSize, pageIndex, flag };
            // 实例化存储过程操作类
            StoreProcedure sp = new StoreProcedure(_con, "leaveWord_reply_select");
            // 执行查询
            SqlDataReader reader = sp.ExecuteDataReader(paramvalues);

            // 遍历SqlDataReader
            while (reader.Read())
            {
                // 添加到留言回复列表
                leaveWordList.Add(new ELeaveWord()
                {
                    RId = Convert.ToInt32(reader["RId"]),
                    LId = Convert.ToInt32(reader["LId"]),
                    Name = Convert.ToString(reader["Name"]),
                    ContactWay = Convert.ToString(reader["ContactWay"]),
                    Content = Convert.ToString(reader["Content"]),
                    Isvalid = Convert.ToBoolean(reader["Isvalid"]),
                    WriteTime = Convert.ToDateTime(reader["WriteTime"])
                });
            }

            reader.NextResult();

            if (reader.Read())
            {
                total = Convert.ToInt32(reader["Total"]);
            }

            reader.Close();
            return leaveWordList;
        }

        #endregion
    }
}
